import { _decorator, Component, Node, NodeEventType, Prefab, tween, v2, v3, Vec2, Vec3} from 'cc';
import { EffectBase } from './EffectBase';
import { EffectClassUtil } from './EffectClassUtil';
import { JokerEffectData } from './EffectBase';
import { resmgr } from './ResMgr';
import { PrefabMgr } from './SpecialResMgr';
import { Mark } from './Mark';
const { ccclass, property } = _decorator;

export enum JokerRarity {
    COMMON = 1,      // 普通
    UNCOMMON = 2,    // 稀有
    RARE = 3,        // 罕见
    LEGENDARY = 4,   // 传奇
    ANCIENT = 5      // 远古
}

@ccclass('Joker')
export class Joker extends Component {
    // 经济属性
    @property({ type: String, displayName: '小丑牌名称', tooltip: '小丑牌的显示名称' })
    protected jokerName: string = "未命名小丑";
    
    @property({ type: String, multiline: true, displayName: '描述', tooltip: '小丑牌的效果描述' })
    protected description: string = "";
    
    @property({ type: Number, displayName: '购买价格', tooltip: '购买这张小丑牌需要的金币' })
    protected buyPrice: number = 0;
    
    @property({ type: Number, displayName: '出售价格', tooltip: '出售这张小丑牌获得的金币' })
    protected sellPrice: number = 0;
    
    @property({ type: Number, displayName: '稀有度', tooltip: '小丑牌的稀有程度' })
    protected rarity: JokerRarity = JokerRarity.COMMON;
        
    @property({ type: Boolean, displayName: '已装备', tooltip: '是否已装备到卡组中' })
    protected isEquipped: boolean = false;
    // 效果相关
    @property({ type: String, displayName: '效果类名', tooltip: '对应的效果类名称' })
    protected effectClassName: string = "";
    
    protected effectInstance: EffectBase | null = null;
    //标签
    private mark:Mark=null;
    //是否点击
    private istouch:boolean=false;
    onLoad() {
        this.initializeEffect();
        this.mark=this.node.parent.getChildByName("Mark").getComponent(Mark);
        this.mark.init(this.buyPrice,this.sellPrice);
        //设置点击事件显示价格
        this.node.on(NodeEventType.MOUSE_DOWN,this.showPrice,this);
    }

    // 初始化效果实例
    protected initializeEffect(): void {
        if (this.effectClassName) {
            this.effectInstance = EffectClassUtil.instance.createEffectInstance(this.effectClassName);
            if (!this.effectInstance) {
                console.error(`无法创建效果实例: ${this.effectClassName}`);
            }
        }
    }
    //点击显示价格（商店显示售卖，牌组显示出售）
    private showPrice(){
        //如果已经被点击，再次点击关闭角标
        if(this.istouch){
            this.mark.closePrice();
            this.istouch=false;
            return;
        }
        //激活角标
        this.istouch=true;
        this.mark.showPrice(this.isEquipped);        
    }

    // 激活小丑牌效果（主要方法）
    public activateEffect(data?: JokerEffectData): boolean {
        if (!this.effectInstance) {
            console.warn("没有可用的效果实例");
            return false;
        }
        // 执行效果
        this.effectInstance.activateEffect(data);
        // 效果激活后的处理
        this.onEffectActivated();
    }

    // 效果激活后的处理
    protected onEffectActivated(): void {
        console.log(`小丑牌 "${this.jokerName}" 效果已激活`);
        this.playActivationEffects();
    }

    // // 播放激活效果（视觉、音效等）
    protected playActivationEffects(): void {
        //小丑牌震动
        tween(this.node)
        .to(0.2,{scale:v3(1.3,1.3),angle:20})
        .to(0.2,{scale:v3(1,1),angle:0})
        .start();
    }

    // 购买小丑牌
    // public purchase(): boolean {
    //     if (this.IsUnlocked) {
    //         console.log("已经拥有这张小丑牌");
    //         return false;
    //     }

    //     if (this.buyPrice > 0) {
    //         const canAfford = this.checkCanAffordPurchase();
    //         if (!canAfford) {
    //             console.log("金币不足，无法购买");
    //             return false;
    //         }
            
    //         // 扣除购买费用
    //         this.deductPurchaseCost();
    //     }

    //     this.unlock();
    //     return true;
    // }


    // // 装备/卸下小丑牌
    // public toggleEquip(): void {
    //     this.isEquipped = !this.isEquipped;
    //     console.log(`${this.jokerName} ${this.isEquipped ? '已装备' : '已卸下'}`);
    // }

    // // 出售小丑牌
    // public sell(): number {
    //     if (!this.isUnlocked) {
    //         console.log("未解锁的小丑牌不能出售");
    //         return 0;
    //     }

    //     const sellValue = this.calculateSellValue();
    //     console.log(`出售 ${this.jokerName} 获得 ${sellValue} 金币`);
        
    //     this.isUnlocked = false;
    //     this.isEquipped = false;
        
    //     return sellValue;
    // }

    // // 计算出售价值（考虑稀有度加成）
    // protected calculateSellValue(): number {
    //     let baseValue = this.sellPrice;
        
    //     // 稀有度加成
    //     const rarityMultiplier = this.getRarityMultiplier();
    //     return Math.floor(baseValue * rarityMultiplier);
    // }

    // // 获取稀有度倍率
    // protected getRarityMultiplier(): number {
    //     switch (this.rarity) {
    //         case JokerRarity.COMMON: return 1.0;
    //         case JokerRarity.UNCOMMON: return 1.2;
    //         case JokerRarity.RARE: return 1.5;
    //         case JokerRarity.LEGENDARY: return 2.0;
    //         case JokerRarity.ANCIENT: return 3.0;
    //         default: return 1.0;
    //     }
    // }

    // Getter 方法
    public get Name(): string { return this.jokerName; }
    public get Description(): string { return this.description; }
    public get BuyPrice(): number { return this.buyPrice; }
    public get SellPrice(): number { return this.sellPrice; }
    public get Rarity(): JokerRarity { return this.rarity; }
    public get IsUnlocked(): boolean { return this.IsUnlocked; }
    public get IsEquipped(): boolean { return this.isEquipped; }
}

